<?php
// 在安装wordpress之前,请先在SAE上创建好应用,并初始化Mysql服务;同时在"数据存储"服务中创建一个domain.
// 然后请修改以下信息
// 应用信息
$GLOBALS['config']['appname'] = ''; // 你创建的appname -> (???).sinaapp.com
$GLOBALS['config']['akey'] = ''; // accesskey
$GLOBALS['config']['skey'] = ''; // secretkey

// 数据库信息
$GLOBALS['config']['db_host'] = '3307.mysql.sae.sina.com.cn'; // 
$GLOBALS['config']['db_port'] = '3307'; // 

// 存储
$GLOBALS['config']['storage_domain'] = ''; // the domain you create in "数据存储" page

// cookie处理
$GLOBALS['config']['cookie_time'] = 0;
$GLOBALS['config']['cookie_path'] = '/';

// !!! 使用自己域名的同学请注释掉下边2行
session_set_cookie_params( c('cookie_time') , c('cookie_path') , c('appname') . '.sinaapp.com'  );
define('COOKIE_DOMAIN', c('appname') . '.sinaapp.com' );

// 是否开启错误输出
//sae_set_display_errors( true );

// ======================================================
// 以下为函数库,不用修改
// ======================================================


function v( $str )
{
	return isset( $_REQUEST[$str] ) ? $_REQUEST[$str] : false;
}

function z( $str )
{
	return strip_tags( $str );
}

function c( $str )
{
	return isset( $GLOBALS['config'][$str] ) ? $GLOBALS['config'][$str] : false;
}

function g( $str )
{
	return isset( $GLOBALS[$str] ) ? $GLOBALS[$str] : false;	
}

function e($message = null,$code = null) 
{
	throw new Exception($message,$code);
}

function t( $str )
{
	return trim($str);
}

// session management
function ss( $key )
{
	return isset( $_SESSION[$key] ) ?  $_SESSION[$key] : false;
}

function ss_set( $key , $value )
{
	return $_SESSION[$key] = $value;
}


function fliter( $array , $pre )
{
	$ret = array();
	foreach( $array as $key=>$value )
	{
		if( strpos( $key , $pre ) === 0 )
			$ret[$key] = $value;
	}
	return $ret;
}

// fetch url
function lz_fetch_url( $url , $time = 0  )
{
	global $rheader,$rdata;
	
	$rheader = '';
	$rdata = '';
	
	$error = '';
	$headers = '';
	
	if( $content = fetch_url( $url , c('akey') , c('skey') , $headers , $error  ))
	{
		$info = lz_header_info( $headers );
		$headers = '';
		
		$jump = false;
		
		if( isset($info['location']) && ( $info['code'] == 301 ) && ($time < 5) ) $jump = true;
		if( isset($info['location']) && ( $info['code'] == 302 ) && ($time < 5) ) $jump = true;
		
		
		if( $jump )
		{
			$time++;
			return lz_fetch_url( $info['location'] , $time );
		}
		else
			return $content; 
			
	}
	else
		return false;
	
	
	
}


function lz_header_info( $header )
{
	$hinfo = array();
	$header_lines = explode( "\r" , trim( $header ) );
	$first = array_shift( $header_lines );
	// HTTP/1.1 301 Moved Permanently
	$reg ="/HTTP\/(.+?)\s([0-9]+)\s(.+)/is";
	if( preg_match( $reg , trim( $first ) , $out ) )
	{
		$hinfo['version'] = $out[1];
		$hinfo['code'] = $out[2];
		$hinfo['code_info'] = $out[3];
	
	}else return false;
	
	if( is_array( $header_lines ) )
	{
		foreach( $header_lines as $line )
		{
			$fs = explode( ":" , trim($line) , 2 );
			if( strlen(trim($fs[0])) > 0 )
				$hinfo[strtolower(trim($fs[0]))] = trim($fs[1]);
		}
	}
	
	return $hinfo;
}


// storage
function lz_put_contents( $file_name , $data , $domain = NULL )
{
	if( $domain === NULL ) $domain = c('storage_domain');
	$storage = sae_storage_init( c('akey') , c('skey') );
	if( !$storage->writeData( $domain , $file_name , $data ) )
		return false;
	else
		return $storage->getFileUrl( $domain , $file_name );
}

function lz_get_contents( $file_name , $domain = NULL )
{
	if( $domain === NULL ) $domain = c('storage_domain');
	$storage = sae_storage_init( c('akey') , c('skey') );
	return $storage->readFile( $domain , $file_name );
}

function lz_get_url( $file_name , $domain = NULL )
{
	if( $domain === NULL ) $domain = c('storage_domain');
	$storage = sae_storage_init( c('akey') , c('skey') );
	return $storage->getFileUrl( $domain , $file_name );
}

// image
function lz_resize_cached( $img_data , $to_file , $width = 100 , $height = 0 , $domain = NULL )
{
	$key = 'lic_' . c('appname') . md5( $img_data );
	if( !$link = lz_mc_get( $key ) )
	{
		$link = lz_resize( $img_data , $to_file , $width , $height , $domain );
		lz_mc_set( $key , $link );
	}	
	return $link;
}


function lz_resize( $img_data , $to_file , $width = 100 , $height = 0 , $domain = NULL )
{
	if( $domain === NULL ) $domain = c('storage_domain');
	$img = sae_image_init( c('akey') , c('skey') , $img_data );
	if( !$new_data = $img->resize( $width , $height ) )
	{
		echo $img->errmsg();
		return false;
	} 
	//echo $new_data;  
	
	return lz_put_contents( $to_file , $new_data , $domain );
}

function lz_fliph( $img_data , $to_file , $domain = NULL )
{
	if( $domain === NULL ) $domain = c('storage_domain');
	$img = sae_image_init( c('akey') , c('skey') , $img_data );
	if( !$new_data = $img->flip_h() ) return false;
	return lz_put_contents( $to_file , $new_data , $domain );
}

function lz_flipv( $img_data , $to_file , $domain = NULL )
{
	if( $domain === NULL ) $domain = c('storage_domain');
	$img = sae_image_init( c('akey') , c('skey') , $img_data );
	if( !$new_data = $img->flip_v() ) return false;
	return lz_put_contents( $to_file , $new_data , $domain );
}

// mc
function mc()
{
	if( !isset( $GLOBALS['LZ_MC'] ) )
		$GLOBALS['LZ_MC'] = memcache_init();
	return $GLOBALS['LZ_MC'];	
}

function lz_mc_get( $key )
{
	return memcache_get( mc() , $key );
}

function lz_mc_set( $key , $value )
{
	return memcache_set( mc() , $key , $value );
}

function s( $str , $db = NULL )
{
	if( $db == NULL ) $db = db();
	return  $db->escape( $str );	
}


function db()
{
	if( !isset( $GLOBALS['LZ_DB'] ) )
		$GLOBALS['LZ_DB'] = sae_mysql_init( c('db_host') , c('db_port') , c('akey') , c('skey') , c('appname') );
	
	return $GLOBALS['LZ_DB'];
}

function get_data( $sql , $db = NULL )
{
	if( $db == NULL ) $db = db();
	return $db->get_data( $sql );
}

function get_line( $sql , $db = NULL )
{
	if( $db == NULL ) $db = db();
	return $db->get_line( $sql );
}

function get_var( $sql , $db = NULL )
{
	if( $db == NULL ) $db = db();
	return $db->get_var( $sql );
}

function last_id( $db = NULL )
{
	if( $db == NULL ) $db = db(); 
	return $db->last_id();
}

function run_sql( $sql , $db = NULL )
{
	if( $db == NULL ) $db = db();
	return $db->run_sql( $sql );
}

function db_errno( $db = NULL )
{
	if( $db == NULL ) $db = db();
	return $db->errno();
}


function db_error()
{
	if( $db == NULL ) $db = db();
	return $db->error();
}

function close_db( $db = NULL )
{
	if( $db == NULL ) $db = db();
	$db->close_db();
}





?>